<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\CourseModel;
use app\model\RedisModel;
use app\model\ShopsModel;
use think\App;

class Courses extends Base {

    protected $middleware = [Auth::class];

    private $aid;

    private $course_id;

    private $courseModel;

    private $req;

    private $page;

    private $limit;

    private $sortType;


    public $cacheKey="course:id:";


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->course_id =  isset($this->req['course_id']) ? $this->req['course_id'] : "";

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;

        $this->sortType = (isset($this->req['sortType']) && !empty($this->req['sortType'])) ? $this->req['sortType'] : "c.created_at";

        $this->courseModel = new CourseModel();

        $this->aid = $this->isLogin();

    }



    /*
     * 获取店铺列表
     * */
    public function getList(){
        $where = [];
        $where[] = ['c.is_delete',"=",0];
        if(!empty($this->req['keyword'])){
            $where[] = ['c.course_name|c.teacher|m.course_menus_name','like',"%".$this->req['keyword']."%"];
        }

        if(!empty($this->req['course_id'])){
            $where[] = ['course_id','=',$this->req['course_id']];
        }

        if(!empty($this->req['base_course_category_id'])){
            $where[] = ['base_course_category_id','=',$this->req['base_course_category_id']];
        }

        if(!empty($this->req['course_menus_id'])){
            $where[] = ['c.course_menus_id','=',$this->req['course_menus_id']];
        }

        if(isset($this->req["course_status"]) && is_numeric($this->req["course_status"]) && in_array($this->req["course_status"],[0,1,-1])){
            $where[] =['course_status',"=",$this->req["course_status"]];
        }

        $orderType = isset($this->req['order']) ? $this->req['order'] : "DESC";


        $list = $this->courseModel->getList($where,$this->page,$this->limit,$this->sortType,$orderType);

        $total =  $this->courseModel->getTotal($where);

        return $this->succData($total,$list);

    }


    /*
     * 获取店铺详情
     * */
    public function getInfo(){

        if(empty($this->course_id)){
            return $this->fail("请选择课程");
        }

        $info = $this->courseModel->getInfo($this->course_id);

        return $this->succ($info);
    }



    public function add()
    {

        if(empty($this->req['course_name']) ){
            return $this->fail("课程名称");
        }

        if(empty($this->req['base_course_category_id'])){
            return $this->fail("请选择课程形式");
        }

        if(empty($this->req['course_menus_id'])){
            return $this->fail("请选择课程类别");
        }


        if(empty($this->req['teacher'])){
            return $this->fail("请填写授课人名称");
        }

        if(empty($this->req['teacher_label'])){
            return $this->fail("请填写授课人头衔");
        }

        if(empty($this->req['description'])){
            return $this->fail("请填写课程介绍");
        }

        if(empty($this->req['description_img'])){
            return $this->fail("详情图不能为空");
        }

        if(empty($this->req['video']) && empty($this->req["tx_vid"])){
            return $this->fail("视频链接和腾讯视频vid 不能同时为空");
        }


        if(empty($this->req['teacher_avatar'])){
            return $this->fail("授课人头像不能为空");
        }

        if(empty($this->req['teacher_desc'])){
            return $this->fail("授课人介绍不能为空");
        }

        if(empty($this->req['course_cover_img'])){
            return $this->fail("课程封面不能为空");
        }


        if( $this->courseModel->insertGetId($this->req)){

           return $this->succ();

       }else{

           return $this->fail();

       }
    }


    public function edit()
    {
        if(empty($this->course_id) ){
            return $this->fail("请选择课程");
        }

        if(empty($this->req['course_name']) ){
            return $this->fail("课程名称");
        }

        if(empty($this->req['base_course_category_id'])){
            return $this->fail("请选择课程形式");
        }

        if(empty($this->req['course_menus_id'])){
            return $this->fail("请选择课程类别");
        }


        if(empty($this->req['teacher'])){
            return $this->fail("请填写授课人名称");
        }

        if(empty($this->req['teacher_label'])){
            return $this->fail("请填写授课人头衔");
        }

        if(empty($this->req['teacher_desc'])){
            return $this->fail("授课人介绍不能为空");
        }

        if(empty($this->req['description'])){
            return $this->fail("请填写课程介绍");
        }
        if(empty($this->req['description_img'])){
            return $this->fail("详情图不能为空");
        }

        if(empty($this->req['video']) && empty($this->req["tx_vid"])){
            return $this->fail("视频链接和腾讯视频vid 不能同时为空");
        }

        if(empty($this->req['teacher_avatar'])){
            return $this->fail("授课人头像不能为空");
        }

        if(empty($this->req['course_cover_img'])){
            return $this->fail("课程封面不能为空");
        }

        unset($this->req['course_id']);

        if( $this->courseModel->where('course_id',$this->course_id)->save($this->req)){

            $this->delCache($this->course_id);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }


    /*
     * 店铺审核
     * */
    public function updateStatus(){
        if(empty($this->course_id)){
            return $this->fail("请选择课程");
        }

        if(!in_array($this->req['course_status'],[-1,0,1])){
            return $this->fail("状态不合法");
        }

        if($this->courseModel->where('course_id',$this->course_id)->save([
            'course_status'=>$this->req['course_status'],
        ])){

            $this->delCache($this->course_id);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }


    /*
     * 删除店铺
     * */
    public function del(){
        if(empty($this->course_id)){
            return $this->fail("请选择课程");
        }


        if($this->courseModel->where('course_id',$this->course_id)->save([
            'is_delete'=>1,
        ])){

            $this->delCache($this->course_id);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }



    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }

    public function delAllCache()
    {

        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }

}
